
package com.carsite.dao.hibernate;

import java.util.List;

import org.hibernate.Query;
import org.springframework.orm.ObjectRetrievalFailureException;

import com.carsite.dao.ModuleMenuDao;
import com.carsite.model.ModuleMenu;
import com.carsite.model.QueryMap;

public class ModuleMenuDaoHibernate extends BaseDaoHibernate implements ModuleMenuDao {

    /**
     * @see com.carsite.dao.ModuleMenuDao#getModuleMenus(com.carsite.model.ModuleMenu)
     */
    public List getModuleMenus(final ModuleMenu moduleMenu) {
        return getHibernateTemplate().find("from ModuleMenu");

        /* Remove the line above and uncomment this code block if you want 
           to use Hibernate's Query by Example API.
        if (moduleMenu == null) {
            return getHibernateTemplate().find("from ModuleMenu");
        } else {
            // filter on properties set in the moduleMenu
            HibernateCallback callback = new HibernateCallback() {
                public Object doInHibernate(Session session) throws HibernateException {
                    Example ex = Example.create(moduleMenu).ignoreCase().enableLike(MatchMode.ANYWHERE);
                    return session.createCriteria(ModuleMenu.class).add(ex).list();
                }
            };
            return (List) getHibernateTemplate().execute(callback);
        }*/
    }

    /**
     * @see com.carsite.dao.ModuleMenuDao#getModuleMenu(Long menuId)
     */
    public ModuleMenu getModuleMenu(final Long menuId) {
    	
        ModuleMenu moduleMenu = (ModuleMenu) getHibernateTemplate().get(ModuleMenu.class, menuId);
        if (moduleMenu == null) {
            log.warn("uh oh, moduleMenu with menuId '" + menuId + "' not found...");
            throw new ObjectRetrievalFailureException(ModuleMenu.class, menuId);
        }

        return moduleMenu;
    }

    /**
     * @see com.carsite.dao.ModuleMenuDao#saveModuleMenu(ModuleMenu moduleMenu)
     */    
    public void saveModuleMenu(final ModuleMenu moduleMenu) {
        getHibernateTemplate().saveOrUpdate(moduleMenu);
    }

    /**
     * @see com.carsite.dao.ModuleMenuDao#removeModuleMenu(Long menuId)
     */
    public void removeModuleMenu(final Long menuId) {
        getHibernateTemplate().delete(getModuleMenu(menuId));
    }

	public List getModuleMenusByParent(String id) {
		// TODO Auto-generated method stub
		String hql = "from ModuleMenu mm where mm.parent=:parent";
		
		if (id.equals("0")) {
			List list=this.getHibernateTemplate().findByNamedParam(hql, "parent",
					null);
			System.out.println("ModuleMenuDaoHiber___list.size:"+list.size());
			return list;
		
		}
		ModuleMenu mm = this.getModuleMenu(new Long(id));
		return this.getHibernateTemplate().findByNamedParam(hql,"parent",mm);
	}

    
	public List getModules() {
		 return getHibernateTemplate().find("from ModuleMenu where parentId is null  order by menuId desc");
	}
	public List getMenus(QueryMap queryMap) {
		String sql = queryMap.getQuerySql();
		sql = "from ModuleMenu "  + sql;
		Query query = getSession().createQuery(sql);				
		return query.list();
	}
	
	
}
